package com.un.ebs.sys.controller;


import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.product.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/file")
@Api(value = "sys-file", description = "文件管理")
public class FileController {

    @Resource
    private FileService service;

    @PostMapping(value = "upload/{pathKey}", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "upload", notes = "上传文件")
    public WebResult upload(@PathVariable(value = "pathKey") String pathKey, MultipartFile file) {
        return WebUtils.success(service.uploadOss(file, pathKey, null));
    }

    @GetMapping(value = "getPathUrl")
    @ApiOperation(value = "getPathUrl", notes = "获取图片路径")
    public WebResult getUrl(@RequestParam(value = "fileKey") String fileKey) {
        return WebUtils.success(service.getOssUrl(fileKey));
    }

    @DeleteMapping("delete/{fileKey}")
    @ApiOperation(value = "delete", notes = "删除")
    public WebResult delete(@PathVariable("fileKey") String fileKey) {
        service.deleteByKey(fileKey);
        return WebUtils.success();
    }
}